/*
 * Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.jet.impl;

import com.hazelcast.jet.config.JobConfig;
import com.hazelcast.jet.core.JobStatus;
import com.hazelcast.jet.impl.execution.init.JetInitDataSerializerHook;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;

import java.io.IOException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;

import static com.hazelcast.jet.core.JobStatus.COMPLETED;
import static com.hazelcast.jet.core.JobStatus.FAILED;
import static com.hazelcast.jet.impl.util.Util.exceptionallyCompletedFuture;
import static com.hazelcast.jet.impl.util.Util.idToString;
import static com.hazelcast.jet.impl.util.Util.toLocalDateTime;
import static java.util.concurrent.CompletableFuture.completedFuture;

public class JobResult implements IdentifiedDataSerializable {

    private String coordinatorUUID;
    private long jobId;
    private JobConfig jobConfig;
    private long creationTime;
    private long completionTime;
    private Throwable failure;

    public JobResult() {
    }

    public JobResult(long jobId, JobConfig jobConfig, String coordinatorUUID, long creationTime, Long completionTime,
                     Throwable failure) {
        this.jobId = jobId;
        this.jobConfig = jobConfig;
        this.coordinatorUUID = coordinatorUUID;
        this.creationTime = creationTime;
        this.completionTime = completionTime;
        this.failure = failure;
    }

    public long getJobId() {
        return jobId;
    }

    public JobConfig getJobConfig() {
        return jobConfig;
    }

    public String getCoordinatorUUID() {
        return coordinatorUUID;
    }

    public long getCreationTime() {
        return creationTime;
    }

    public long getCompletionTime() {
        return completionTime;
    }

    public boolean isSuccessful() {
        return (failure == null);
    }

    public boolean isSuccessfulOrCancelled() {
        return (failure == null || failure instanceof CancellationException);
    }

    public Throwable getFailure() {
        return failure;
    }

    public JobStatus getJobStatus() {
        return isSuccessfulOrCancelled() ? COMPLETED : FAILED;
    }

    public CompletableFuture<Void> asCompletableFuture() {
        return failure == null ? completedFuture(null) : exceptionallyCompletedFuture(failure);
    }

    @Override
    public String toString() {
        return "JobResult{" +
                "coordinatorUUID='" + coordinatorUUID + '\'' +
                ", jobId=" + idToString(jobId) +
                ", jobConfig.name=" + jobConfig.getName() +
                ", creationTime=" + toLocalDateTime(creationTime) +
                ", completionTime=" + toLocalDateTime(completionTime) +
                ", failure=" + failure +
                '}';
    }

    @Override
    public int getFactoryId() {
        return JetInitDataSerializerHook.FACTORY_ID;
    }

    @Override
    public int getId() {
        return JetInitDataSerializerHook.JOB_RESULT;
    }

    @Override
    public void writeData(ObjectDataOutput out) throws IOException {
        out.writeLong(jobId);
        out.writeObject(jobConfig);
        out.writeUTF(coordinatorUUID);
        out.writeLong(creationTime);
        out.writeLong(completionTime);
        out.writeObject(failure);
    }

    @Override
    public void readData(ObjectDataInput in) throws IOException {
        jobId = in.readLong();
        jobConfig = in.readObject();
        coordinatorUUID = in.readUTF();
        creationTime = in.readLong();
        completionTime = in.readLong();
        failure = in.readObject();
    }

}
